package com.z.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.z.admin.dao.DanmuMapper;
import com.z.admin.entity.Danmu;
import com.z.admin.service.IDanmuService;
import com.z.common.base.ResponseCode;
import com.z.common.base.Results;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author z
 * @since 2022-03-13
 */
@Service
public class DanmuServiceImpl extends ServiceImpl<DanmuMapper, Danmu> implements IDanmuService {

    @Resource
    private DanmuMapper DanmuMapper;
    @Override
    public Results<Danmu> findPageByMap(Map<String, Object> map, Integer offset, Integer limit) {
        StringBuffer where = new StringBuffer("");
        if(map != null && map.size() > 0){
            where.append("where ");
            for(Map.Entry<String, Object> entry : map.entrySet()){
                String[] strArr = entry.getKey().split("_");
                where.append(strArr[0]+" ");
                if(strArr[1].equals("like")){
                    where.append("like '%" +entry.getValue()+"%' and ");
                }else{
                    where.append("= '" +entry.getValue()+"' and ");
                }
            }
            where.delete(where.length()-4, where.length());
        }
        return Results.success(DanmuMapper.conutByMap(where.toString()), DanmuMapper.findPageByWhereSql(where.toString(), offset, limit));
    }

    @Override
    public Results delAll(String ids) {
        try{
            for(String id : ids.split(",")) {
                DanmuMapper.deleteById(id);
            }
        }catch (Exception e){
            e.printStackTrace();
            return Results.failure(ResponseCode.error.getCode(), e.getMessage());
        }
        return Results.success();
    }
}
